/********************************************************
Code to create the predicted probabilities for Figure OB3. 
Also creates the Tables for these results. 

***********************************************************/

/*******************************************
Pew/CNN
*******************************************/


/**************************
		Data
**************************/
clear
*change directory to "....\Data\"
*cd 
use "pew_cnn_comb.dta"
set more off


/**************************
		Cleaning
Some of the labels are not right 
**************************/
**Dem and Rep Extreme
label var dem_extreme "Democratic Party: Too Extreme?"
label var rep_extreme "Republican Party: Too Extreme?"
label def extreme 1 "Too Extreme" 0 "Not Too Extreme/DK"
label values dem_extreme rep_extreme extreme

**PID & Ideology - centered so that 0 = Pure Independent/Moderate
gen pid1 = pid - 3
label var pid1 "Party Identification"

gen ideol1 = ideol - 3
label var ideol1 "Symbolic Ideology"

**Income
label def inc1 1 "<30,000" 2 "30,000-49,999" 3 "50,000-74,999" 4 "75,000-99,999" 5 "100,000+"
label values income1 inc1
label var income1 "Income"

label def inc2 1 "<30,000" 2 "30,000-49,999" 3 "50,000-74,999" 4 "75,000+"
label values income2 inc2
label var income2 "Income"

**Gender
label def gend 1 "Female" 0 "Male"
label values gender gend
label var gender "Gender"

**Education 
label var educ "Education"
label def edu 1 "<HS" 2 "HS" 3 "Some College" 4 "BA" 5 "Post-BA"
label values educ edu

**Race/Ethnicity
*Remove the 9
recode race_eth (9 = .)
label var race_eth "Race/Ethnicity"
label def rac 1 "White" 2 "Black" 3 "Hispanic" 4 "Other Race"
label values race_eth rac

**Age
label var age "Age"

**Sponsor
encode sponsor, gen(sponsor1)

**Year
label var year "Year of Survey"


/**************************
	Model and Predictions
**************************/

eststo clear
*Republican Party: Too Extreme?
eststo: melogit rep_extreme c.pid1##c.ideol1 c.age##c.age i.gender i.educ i.income2 i.race_eth i.sponsor1 c.year [pweight=weight] || surveyid: 
	
	*Predictions
	margins, at(pid1=(-2 0 2) ideol1=(-2(1)2)) saving(pew_cnn_stata_rep_predictions, replace)
	
*Democratic Party: Too Extreme? 
eststo: melogit dem_extreme c.pid1##c.ideol1 c.age##c.age i.gender i.educ i.income2 i.race_eth i.sponsor1 c.year [pweight=weight] || surveyid: 
	
	*Predictions
	margins, at(pid1=(-2 0 2) ideol1=(-2(1)2)) saving(pew_cnn_stata_dem_predictions, replace)
	
***Random Effects for PID and Ideology
eststo: melogit rep_extreme c.pid1##c.ideol1 c.age##c.age i.gender i.educ i.income2 i.race_eth i.sponsor1 c.year [pweight=weight] || surveyid: pid1 ideol1
eststo: melogit dem_extreme c.pid1##c.ideol1 c.age##c.age i.gender i.educ i.income2 i.race_eth i.sponsor1 c.year [pweight=weight] || surveyid: pid1 ideol1


/**************************
		Table
**************************/

esttab using "table_ob1.rtf", replace onecell se aic bic label nobaselevels ///
	mtitles("Republican Party" "Democratic Party" "Republican Party" "Democratic Party") ///
	title("{\b Table OB1:} Perceptions of Party Extremity, Pew & CNN Data") ///
	addnotes("Results use survey weights. PID and Ideology range from -2 to +2; 0 indicates either a pure independent or a moderate.") 

eststo clear


/*******************************************
Lucid
*******************************************/

clear
use "lucid_coded_all.dta"
set more off



/**************************
		Cleaning

Some of the labels are not right 
**************************/

****PID
gen pid1 = partyid - 4
label var pid1 "Party Identification"

****Ideology
*Symbolic
gen ideol1 = self_ideol - 4
label var ideol1 "Symbolic Ideology"

*Operational
label var op_ideol "Operational Ideology"

*Proximity
gen dem_prox = abs(self_ideol - dem_ideol)
label var dem_prox "Proximity: Democratic Party"

gen rep_prox = abs(self_ideol - rep_ideol)
label var rep_prox "Proximity: Republican Party"

****Political Knowledge
label var knowl_sum "Political Knowledge"

****Opinionation
label var opinionation "Opinionation"

****Demographics
*Age
label var age "Age"

*Gender
label var gender "Gender"

*Race
label def rac 1 "White" 2 "Black" 3 "Hispanic" 4 "Asian" 5 "Other Race"
label values race_eth rac
label var race_eth "Race/Ethnicity"

*Education
label def edu 1 "<HS" 2 "HS" 3 "Some College" 4 "BA" 5 "BA+"
label values educ edu

*Income
label var house_income "Income"

*Weight
label var weight "Raked Survey Weight"

****Correlations
pwcorr pid1 ideol1 op_ideol dem_prox rep_prox


/**************************
	Models & Tables
**************************/

eststo clear

***PID*Symbolic
eststo clear
eststo: logit rep_extreme_1 c.pid1##c.ideol1 knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) ideol1=(-3(1)3)) saving(lucid_rep_sym, replace)
		margins, dydx(ideol1) at(pid1=(-3(1)3))

eststo: logit dem_extreme_1 c.pid1##c.ideol1 knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) ideol1=(-3(1)3)) saving(lucid_dem_sym, replace)

*Table
esttab using "table_ob2.rtf", replace onecell se label nobaselevels pr2 ///
	mtitles("Republican Party (Unweighted)" "Republican Party (Weighted)" "Democratic Party (Unweighted)" "Democratic Party (Weighted)") ///
	title("{\b Table OB2:} Perceptions of Party Extremity, Lucid Data (Symbolic Ideology Models)")

eststo clear
	
***PID*Operational

eststo clear
eststo: logit rep_extreme_1 c.pid1##c.op_ideol knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) op_ideol=(-5(1)5)) saving(lucid_rep_op, replace)

eststo: logit dem_extreme_1 c.pid1##c.op_ideol knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) op_ideol=(-5(1)5)) saving(lucid_dem_op, replace)
		
esttab using "table_ob3.rtf", replace onecell se label nobaselevels pr2 ///
	mtitles("Republican Party (Unweighted)" "Republican Party (Weighted)" "Democratic Party (Unweighted)" "Democratic Party (Weighted)") ///
	title("{\b Table OB3:} Perceptions of Party Extremity, Lucid Data (Operational Ideology Models)")

***PID*Proximity

*Reverse Order of proximity variable

eststo clear
eststo: logit rep_extreme_1 c.pid1##c.rep_prox knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) rep_prox=(0(1)6)) saving(lucid_rep_prox, replace)
		
		margins, at(pid1=(-3 0 3) rep_prox==0) post coeflegend
		lincom _b[1bn._at] -  _b[2._at]
		lincom _b[1bn._at] - _b[3._at]
		
eststo: logit dem_extreme_1 c.pid1##c.dem_prox knowl_sum opinionation c.age##c.age house_income i.gender i.race_eth i.educ 
		
		margins, at(pid1=(-3 0 3) dem_prox=(0(1)6)) saving(lucid_dem_prox, replace)
		
	
		margins, at(pid1=(-3 0 3) dem_prox==0) post coeflegend
				lincom _b[3._at] -  _b[1bn._at]
				lincom _b[3._at] - _b[2._at]
	
esttab using "table_ob4.rtf", replace onecell se label nobaselevels pr2 ///
	mtitles("Republican Party (Unweighted)" "Republican Party (Weighted)" "Democratic Party (Unweighted)" "Democratic Party (Weighted)") ///
	title("{\b Table OB4:} Perceptions of Party Extremity, Lucid Data (Ideological Proximity Models)") ///
	rename(rep_prox Proximity c.pid1#c.rep_prox PID#Proximity dem_prox Proximity c.pid1#c.dem_prox PID#Proximity) ///
	addnotes("Proximity is the absolute distance between the respondent's self-placement and their placement of the party. 0 thus indicate maximium proximity.")

